home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / nxyplot / Source / PlotDelegate.m < prev    next >
Text File  |  1994-02-01  |  7KB  |  282 lines

  1.  
  2. /* Generated by Interface Builder */
  3.  
  4. /*
  5.  * Some of this code is essentially copied from the AtYourService example
  6.  * of Henry Krempel.
  7.  */
  8.  
  9. #import "PlotDelegate.h"
  10. #import "ServicesHandler.h"
  11. #import "Plot.h"
  12. #import "FormatHandler.h"
  13. #import "PlotView.h"
  14. #import "ScrollWindow.h"             // added 10-7-91 by pdhowell
  15. #import <strings.h>
  16. #import <appkit/appkit.h>
  17. #import <defaults/defaults.h>
  18. #import <streams/streams.h>
  19. #import <sys/types.h>        /* only needed in the iconReleasedAt::: code */
  20. #import <sys/stat.h>        /* ditto */
  21.  
  22. @implementation PlotDelegate
  23.  
  24. - appWillInit:sender
  25. {
  26.   // We used to do this in appDidInit, but ran into trouble when we got
  27.   // the Workspace Manager to automatically run nxyplot on files with a
  28.   // selected extension.  The problem was that the 3 windows which get
  29.   // turned into ScrollWindows by the makeSomeScrollWindows method had not been
  30.   // turned into ScrollWindows when a message was sent to them that only
  31.   // a ScrollView (not an ordinary View) could understand.  The solution seems
  32.   // to be to make them into ScrollWindows very early in the game, in the
  33.   // appWillInit method.
  34.   [plotParam makeSomeScrollWindows];
  35.  
  36.   NXUpdateDefaults();
  37.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  38.     [plotParam colorOn:YES];
  39.     [colorOptionButton setState:1];
  40.   }
  41.   else {
  42.     [plotParam colorOn:NO];
  43.     [colorOptionButton setState:0];
  44.   }
  45.   if (strncmp(NXGetDefaultValue("nxyplot", "colorPrinting"), "YES", 3) == 0) {
  46.     [printColorButton setState:1];
  47.     [accPrintColorButton setState:1];
  48.   }
  49.   else {
  50.     [printColorButton setState:0];
  51.     [accPrintColorButton setState:0];
  52.   }
  53.   if (strncmp(NXGetDefaultValue("nxyplot", "cycleLineStyles"), "YES", 3) == 0) {
  54.     [printLineStyleButton setState:1];
  55.     [accPrintLineStyleButton setState:1];
  56.   }
  57.   else {
  58.     [printLineStyleButton setState:0];
  59.     [accPrintLineStyleButton setState:0];
  60.   }
  61.   if (strncmp(NXGetDefaultValue("nxyplot", "opaqueBackground"), "YES", 3) == 0) {
  62.     [opaqueBackgroundButton setState:0];
  63.   }
  64.   else {
  65.     [opaqueBackgroundButton setState:1];
  66.   }
  67.   if (strncmp(NXGetDefaultValue("nxyplot", "autoPlot"), "YES", 3) == 0) {
  68.     [autoPlotButton setState:0];
  69.   }
  70.   else {
  71.     [autoPlotButton setState:1];
  72.   }
  73.  
  74.   return self;
  75. }
  76.  
  77. - appDidInit:sender
  78. {
  79.  
  80.   [[NXApp appListener] setServicesDelegate:self];
  81.   [servicesHandler serviceSetState];
  82.  
  83.   [canvasWindow setMiniwindowIcon:"nxyplot.tiff"];
  84.   [controlPanel setMiniwindowIcon:"nxyplot.tiff"];
  85.  
  86.   /*
  87.    * Look for command line arguments (this used to be in nxyplot_main.m).
  88.    */
  89.   if (NXArgc > 1) {
  90.     [self getArgs];
  91.   }
  92.  
  93.   return self;
  94. }
  95.  
  96. - resetDefaults:sender
  97. {
  98.   if ([colorOptionButton state] == 0) {
  99.     NXWriteDefault("nxyplot", "colorOption", "NO");
  100.     [plotParam colorOn:NO];
  101.   }
  102.   else {
  103.     NXWriteDefault("nxyplot", "colorOption", "YES");
  104.     [plotParam colorOn:YES];
  105.   }
  106.   if ([printColorButton state] == 0) {
  107.     NXWriteDefault("nxyplot", "colorPrinting", "NO");
  108.     [accPrintColorButton setState:0];
  109.   }
  110.   else {
  111.     NXWriteDefault("nxyplot", "colorPrinting", "YES");
  112.     [accPrintColorButton setState:1];
  113.   }
  114.   if ([printLineStyleButton state] == 0) {
  115.     NXWriteDefault("nxyplot", "cycleLineStyles", "NO");
  116.     [accPrintLineStyleButton setState:0];
  117.   }
  118.   else {
  119.     NXWriteDefault("nxyplot", "cycleLineStyles", "YES");
  120.     [accPrintLineStyleButton setState:1];
  121.   }
  122.   if ([opaqueBackgroundButton state] == 0) {
  123.     NXWriteDefault("nxyplot", "opaqueBackground", "YES");
  124.   }
  125.   else {
  126.     NXWriteDefault("nxyplot", "opaqueBackground", "NO");
  127.   }
  128.   if ([autoPlotButton state] == 0) {
  129.     NXWriteDefault("nxyplot", "autoPlot", "YES");
  130.   }
  131.   else {
  132.     NXWriteDefault("nxyplot", "autoPlot", "NO");
  133.   }
  134.  
  135.   return self;
  136. }
  137.  
  138.  
  139. /* This method takes data from a pasteboard and sends it to be plotted. */
  140. - plotFromPasteboard:pb userData:(const char *)userData error:(char **)errorMessage
  141. {
  142.   char *data;
  143.   int length;
  144.   const char *const *types;
  145.   int hasAscii, i;
  146.  
  147.   types = [pb types];
  148.   hasAscii = 0;
  149.   for (i = 0; !hasAscii && types[i] ; i++) 
  150.     if (!strcmp(types[i], NXAsciiPboardType)
  151.     || !strcmp(types[i], NXTabularTextPboardType)) hasAscii = 1;
  152.  
  153.   if (hasAscii) {
  154.     [pb readType:NXAsciiPboardType data:&data length:&length];
  155.  
  156.     if (data && length) {
  157.       NXStream *dataStream = NXOpenMemory(data, length, NX_READONLY);
  158.       if ([plotParam readData:dataStream :"pasteboard"] == 0) {
  159.     NXRunAlertPanel("Read", "Couldn't read any data from %s", "OK",
  160.             NULL, NULL, "pasteboard");
  161.       }
  162.       [plotParam plotPrepAndDraw];
  163.     }
  164.     else *errorMessage = "No data in your selection.";
  165.   } 
  166.   else *errorMessage = "No ASCII text found in your selection.";
  167.  
  168.   return self;
  169. }
  170.  
  171. // The next 2 methods suggested by Ralph Zazula.  They are needed to get
  172. // WorkspaceManager automatically to run nxyplot when you double-click on
  173. // files with our registered extension(s).
  174.  
  175. - (BOOL)appAcceptsAnotherFile:sender
  176. {
  177.   return YES;
  178. }
  179.  
  180. - (int)app:sender openFile:(const char *)fullPath type:(const char *)aType
  181. {
  182.   [plotParam openFile:(char *)fullPath :(char *)fullPath];
  183.   return YES;
  184. }
  185.  
  186. - getArgs
  187. {
  188.   int i, formatindex = 0, psindex = 0;
  189.   NXStream *formatStream;
  190.   BOOL quit = NO;
  191.  
  192.   [plotParam makeSomeScrollWindows]; /* must do this here! */
  193.   NXUpdateDefaults();        /* ditto */
  194.   if (strncmp(NXGetDefaultValue("nxyplot", "colorOption"), "YES", 3) == 0) {
  195.     [plotParam colorOn:YES];
  196.     [colorOptionButton setState:1];
  197.   }
  198.   else {
  199.     [plotParam colorOn:NO];
  200.     [colorOptionButton setState:0];
  201.   }
  202.  
  203.   i = 1;
  204.   while (i<NXArgc) {
  205.     if (NXArgv[i][0] == '-') {
  206.       if (NXArgv[i][1] == 'f') { /* format file */
  207.     formatindex = ++i;
  208.       }
  209.       else if (NXArgv[i][1] == 'o') { /* eps file */
  210.     psindex = ++i;
  211.       }
  212.       else if (NXArgv[i][1] == 'q') { /* quit after plotting */
  213.     i++;
  214.     quit = YES;
  215.       }
  216.       else if (NXArgv[i][1] == 'h') {
  217.     NXLogError("usage: nxyplot file [file...] [-f formatfile] [-o epsfile] [-q] [-h]\n");
  218.     exit(0);
  219.       }
  220.       else
  221.     NXLogError("Unrecognized option %s\n", NXArgv[i++]);
  222.     }
  223.     else {
  224.       if (NXMapFile(NXArgv[i], NX_READONLY) == NULL) {
  225.     NXLogError("Unable to open file %s\n", NXArgv[i]);
  226.       }
  227.       else {
  228.     [plotParam openFile:NXArgv[i] :NXArgv[i]];
  229.       }
  230.     }
  231.     i++;
  232.   }
  233.  
  234.   if (formatindex > 0)
  235.     if ((formatStream = NXMapFile(NXArgv[formatindex], NX_READONLY)) == NULL)
  236.       NXLogError("Unable to open format file %s\n", NXArgv[formatindex]);
  237.     else
  238.       [formatHandler readFormatData:formatStream];
  239.  
  240.   if (psindex > 0)
  241.     [canvas savePSCode:NXArgv[psindex]];
  242.  
  243.   if (quit) {
  244.     NXLogError("nxyplot: quit option selected\n");
  245.     [NXApp free];
  246.     exit(0);
  247.   }
  248.   else
  249.     [plotParam drawPlot:self];
  250.  
  251.   return self;
  252. }
  253.  
  254. // Next method copied from Garfinkel and Mahoney (Chapter 6)
  255. - showInfo:sender
  256. {
  257.   if (infoPanel == nil) {
  258.     if (![NXApp loadNibSection: "Info.nib"
  259.         owner:self
  260.         withNames: NO]) {
  261.       return nil;        // load failed
  262.     }
  263.   }
  264.   [infoPanel makeKeyAndOrderFront:nil];
  265.   return self;
  266. }
  267.  
  268. - showHelp:sender
  269.   if (helpPanel == nil) {
  270.     if (![NXApp loadNibSection: "help.nib"
  271.         owner:self
  272.         withNames: NO]) {
  273.       return nil;        // load failed
  274.     }
  275.   }
  276.   [helpPanel makeKeyAndOrderFront:nil];
  277.   return self;
  278. }
  279.  
  280. @end
  281.